package Project;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
/**
 * This class is to customize the menu of this game.
 * 
 * @author	Brian Wetherbee
 * 			Ryan McNamara
 * 			Zeng Yang
 *
 */
public class CustomizationMenu extends Menu {
	/*@ public invariant Main.UI = this; @*/
	private UI prevUI;
	private int selectStatus;
	private int subSelect;
	
	public CustomizationMenu(UI prevUI) {
		super(3);
		this.prevUI = prevUI;
		selectStatus = 0;
		subSelect = 0;
		initComponents();
	}

	private void initComponents()
	{
		addKeyListener(this);
		addMouseListener(this);
		repaint();
	}
	
	@Override
	public void paint(Graphics g)
	{
		super.paint(g);
		
		
		g.setColor(new Color(64,64,64));
		g.fillRect(0, 0, 1672, 968);
		
		g.setColor(new Color(128,128,128));
		g.drawRect(0, 0, 1672, 64);
		g.drawRect(0, 64, 418, 904);
		g.drawRect(418, 64, 1254, 904);
		
		g.setColor(Color.WHITE);
		FontMetrics f = g.getFontMetrics(new Font("DialogInput",1,48));
		g.setFont(new Font("DialogInput",1,48));
		
		int stringX = (int)Main.actualToInternalX(f.stringWidth("Game Settings"));
    	int stringY = (int)Main.actualToInternalX(f.getHeight());
    	int printX = Constants.width / 2 - (stringX / 2);
    	int printY = 48 - ((64 - stringY)/2);
    	g.drawString("Game Settings", printX, printY);
    	
    	g.setFont(new Font("DialogInput",1,24));
    	g.drawString("Controls",16,112);
    	g.drawString("Screen Size",16,160);
    	g.drawString("Exit",16,936);
    	
    	drawOption(g);
    	drawSelector(g);
	}
	
	private void drawOption(Graphics g) {
		g.setColor(new Color(64,64,64));
		g.fillRect(418, 64, 1254, 904);
		g.setColor(new Color(128,128,128));
		g.drawRect(418, 64, 1254, 904);
		
		if(getOption() == 0) {
			g.setColor(Color.WHITE);
			g.drawString("Up",434,112);
			g.drawString("Down",434,160);
			g.drawString("Left",434,208);
			g.drawString("Right",434,256);
			g.drawString("Confirm",434,304);
			g.drawString("Cancel",434,352);
			g.drawString("Change Settings",434,400);
			g.drawString("Upgrade Weapons",434,448);
			g.drawString("Default Controls",434,888);
			g.drawString("Save Controls (changes will take effect immediately)",434,936);
			g.drawString(KeyEvent.getKeyText(SaveFile.getCurrentFile().controlUp), 900, 112);
			g.drawString(KeyEvent.getKeyText(SaveFile.getCurrentFile().controlDown), 900, 160);
			g.drawString(KeyEvent.getKeyText(SaveFile.getCurrentFile().controlLeft), 900, 208);
			g.drawString(KeyEvent.getKeyText(SaveFile.getCurrentFile().controlRight), 900, 256);
			g.drawString(KeyEvent.getKeyText(SaveFile.getCurrentFile().controlConfirm), 900, 304);
			g.drawString(KeyEvent.getKeyText(SaveFile.getCurrentFile().controlCancel), 900, 352);
			g.drawString(KeyEvent.getKeyText(SaveFile.getCurrentFile().controlCustomize), 900, 400);
			g.drawString(KeyEvent.getKeyText(SaveFile.getCurrentFile().controlUpgrade), 900, 448);
		} else if(getOption() == 1) {
			
		}
	}
	
	private void drawSelector(Graphics g) {
		if(selectStatus == 0)
		{
			g.setColor(new Color(64,64,64));
			g.fillRect(0, 64, 418, 904);
			g.setColor(new Color(128,128,128));
			g.drawRect(0, 64, 418, 904);
			
			g.setColor(Color.WHITE);
			g.drawString("Controls",16,112);
	    	g.drawString("Screen Size",16,160);
	    	g.drawString("Exit",16,936);
	    	
			if(getOption() == 0) {
				g.drawRect(12, 82, 200, 36);
			} else if(getOption() == 1) {
				g.drawRect(12, 130, 200, 36);
			} else if(getOption() == 2) {
				g.drawRect(12, 906, 200, 36);
			}
			return;
		} 
		if(selectStatus == 1){
			if(getOption() == 0) {
				g.setColor(Color.WHITE);
				g.drawRect(12, 82, 200, 36);
				
				if(subSelect == 0) {
					g.drawRect(430,82,224,36);
				} else if(subSelect == 1) {
					g.drawRect(430,130,224,36);
				} else if(subSelect == 2) {
					g.drawRect(430,178,224,36);
				} else if(subSelect == 3) {
					g.drawRect(430,226,224,36);
				} else if(subSelect == 4) {
					g.drawRect(430,274,224,36);
				} else if(subSelect == 5) {
					g.drawRect(430,322,224,36);
				} else if(subSelect == 6) {
					g.drawRect(430,370,224,36);
				} else if(subSelect == 7) {
					g.drawRect(430,418,224,36);
				} else if(subSelect == 8) {
					g.drawRect(430,858,224,36);
				} else if(subSelect == 9) {
					g.drawRect(430,906,224 + 600,36);
				}
			}
			return;
		}
		if(selectStatus == 2){
			if(getOption() == 0) {
				g.setColor(Color.WHITE);
				g.drawRect(12, 82, 200, 36);
				
				if(subSelect == 0) {
					g.drawRect(430 + 466,82,224,36);
				} else if(subSelect == 1) {
					g.drawRect(430 + 466,130,224,36);
				} else if(subSelect == 2) {
					g.drawRect(430 + 466,178,224,36);
				} else if(subSelect == 3) {
					g.drawRect(430 + 466,226,224,36);
				} else if(subSelect == 4) {
					g.drawRect(430 + 466,274,224,36);
				} else if(subSelect == 5) {
					g.drawRect(430 + 466,322,224,36);
				} else if(subSelect == 6) {
					g.drawRect(430 + 466,370,224,36);
				} else if(subSelect == 7) {
					g.drawRect(430 + 466,418,224,36);
				}
			}
			return;
		}
	}
	@Override
	public void select() {
		//2 is the max depth in this menu		
		
		if(selectStatus == 0) {
			if(getOption() == 2) {
				exitMenu();
			} else {
				selectStatus = 1;
				repaint();
			}
			return;
		} 
		if(selectStatus == 1){
			if(getOption() == 0) {
				if(subSelect <= 7) {
					this.selectStatus++;					
				} else if(subSelect == 8) {
					//set default
					SaveFile.setDefaultConstrols();
					SaveFile.saveControlsToConstants();
				} else if(subSelect == 9) {
					//save the controls
					SaveFile.saveControlsToConstants();
				}
			}
			repaint();
			return;
		}
		
		if(selectStatus == 2){
			//should never happen
			return;
		}
			
			
		repaint();
		
	}
	
	public void exitMenu() {
		Main.setCurrentUI(prevUI);
	}
	
	public void deselect() {
		if(selectStatus == 0){
			exitMenu();
		} else {
			if(selectStatus == 1){
				if(getOption() == 0){
					Constants.saveControlsToSaveFile();
				}
			}
			selectStatus--;
		}
		repaint();
	}
	
	@Override
	public void increaseHighlight() {
		if(selectStatus == 0) {
			super.increaseHighlight();
		} 
		if (selectStatus == 1) {
			if (getOption() == 0) {
				subSelect = (subSelect + 1) % 10;
			} else if (getOption() == 1) {
				subSelect = (subSelect + 1) % 5;
			}
		}
		if(selectStatus == 2){
			// do nothing, awaiting input from user
		}
		repaint();
	}
	
	@Override
	public void decreaseHighlight() {
		if(selectStatus == 0) {
			super.decreaseHighlight();
		} 
		if(selectStatus == 1){
			if(getOption() == 0){
				//controls sub menu
				subSelect = (subSelect - 1) % 10;
				if(subSelect == -1)
				{
					//there are 8 controls and 2 options
					subSelect = 9;
				}
			}
			if(getOption() == 1){
				//resolution sub menu
				subSelect = (subSelect - 1) % 5;
				if(subSelect == -1)
				{
					subSelect = 5;
				}
			}
		}
		if(selectStatus == 2){
			//shouldn't happen
		}
			
		repaint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(selectStatus == 2){
			if(getOption() == 0){
				changeControls(e);
			}
			return;
		}
		if(e.getKeyCode() == Constants.controlDown) {
			increaseHighlight();
		}
		if(e.getKeyCode() == Constants.controlUp) {
			decreaseHighlight();
		}
		if(e.getKeyCode() == Constants.controlConfirm) {
			select();
		}
		if(e.getKeyCode() == Constants.controlCancel) {
			deselect();
		}
		if(e.getKeyCode() == Constants.controlQuit) {
			System.exit(0);
		}
		System.out.println("option is: " + this.getOption());
		System.out.println("selectStatus is: " + this.selectStatus);
		System.out.println("subselect is: " + this.subSelect);
	}
	private void changeControls(KeyEvent e) {
		if(this.subSelect == 0){
			SaveFile.getCurrentFile().controlUp = e.getKeyCode();
		}
		if (this.subSelect == 1) {
			SaveFile.getCurrentFile().controlDown = e.getKeyCode();
		}
		if (this.subSelect == 2) {
			SaveFile.getCurrentFile().controlLeft = e.getKeyCode();
		}
		if (this.subSelect == 3) {
			SaveFile.getCurrentFile().controlRight = e.getKeyCode();
		}
		if (this.subSelect == 4) {
			SaveFile.getCurrentFile().controlConfirm = e.getKeyCode();
		}
		if (this.subSelect == 5) {
			SaveFile.getCurrentFile().controlCancel = e.getKeyCode();
		}
		if (this.subSelect == 6) {
			SaveFile.getCurrentFile().controlCustomize = e.getKeyCode();
		}
		if (this.subSelect == 7) {
			SaveFile.getCurrentFile().controlUpgrade = e.getKeyCode();
		}
		this.selectStatus--;
		repaint();
	}
	
	

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	
}
